﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using System.Windows.Navigation;
using OnlineVideos.UI;
using OnlineVideos.Common;
using OnlineVideos.Entities;
using System.ComponentModel;
using OnlineVideos.Data;
using OnlineVideos.Utilities;
using Common.Utilities;
using Microsoft.Phone.Controls;
namespace OnlineVideos
{
    public partial class ShowHistory : UserControl
    {
        History objHistory = new History();
        List<LinkHistory> showHistoryVideos = null;
        string link = string.Empty;
        int movieLastIndex = 1;
        int movieFirstIndex = 1;
       
       
        int moviesHisCount = 0;
      public ShowHistory()
        {
            InitializeComponent();
            showHistoryVideos = new List<LinkHistory>();
        }

        private void lbxVidoes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxVidoes.SelectedIndex == -1)
                    return;
                AppSettings.LinkTitle = (lbxVidoes.SelectedItem as LinkHistory).Title.ToString();
                AppSettings.LinkType = "Songs";
                if (!AppResources.ShowFavoritesPageMoviesPivot)
                {
                    List<ShowLinks> objsong = OnlineShow.GetShowLinksByTypeForWp8((lbxVidoes.SelectedItem as ShowLinks).ShowID.ToString(), LinkType.Movies);
                    foreach (ShowLinks objmovie in objsong)
                    {
                        link = objmovie.LinkUrl;
                    }
                    //YouTube.LoadYoutubeVideo(link);
                    PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
                    Page p = frame.Content as Page;
                    p.NavigationService.Navigate(new Uri("/Views/Youtube.xaml?myid=" + link, UriKind.Relative));
                    //YoutubeApi.Youtube.PlayYoutubeVideo(link);
                }
                else
                {
                    if (!AppResources.IsSimpleDetailPage)
                        PageHelper.NavigateTo(NavigationHelper.getMovieDetailPage(lbxVidoes.SelectedItem as INavigationContext));
                    else
                    {
                        if (ResourceHelper.AppName == Apps.Video_Mix.ToString() || ResourceHelper.AppName == Apps.Web_Tile.ToString())
                            PageHelper.NavigateTo(NavigationHelper.getMovieDetailPageForMix(lbxVidoes.SelectedItem as LinkHistory));
                        else
                            PageHelper.NavigateTo(NavigationHelper.getMovieDetailPage(lbxVidoes.SelectedItem as INavigationContext));
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxVidoes_SelectionChanged Method In ShowHistory file.", ex);
            }
            lbxVidoes.SelectedIndex = -1;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                    movieLastIndex = objHistory.GetLastHisId(Constants.MovieHistoryFile);
                    movieFirstIndex = movieFirstIndex - 14;
                    if (movieFirstIndex < 0)
                        movieFirstIndex = 1;
                    moviesHisCount = 15;
                    BackgroundHelper bwHelper = new BackgroundHelper();
                    bwHelper.AddBackgroundTask(
                                                       (object s, DoWorkEventArgs a) =>
                                                       {
                                                           a.Result = objHistory.LoadMovieHistory(movieFirstIndex, movieLastIndex, moviesHisCount);
                                                       },
                                                       (object s, RunWorkerCompletedEventArgs a) =>
                                                       {


                                                           showHistoryVideos = (List<LinkHistory>)a.Result;
                                                           LinkHistory linkhistory = new LinkHistory();
                                                    
                                                           if (showHistoryVideos.Count > 0)
                                                           {
                                                               lbxVidoes.ItemsSource = showHistoryVideos;
                                                               tblkMoviesHistory.Visibility = Visibility.Collapsed;
                                                           }
                                                           else
                                                           {
                                                               tblkMoviesHistory.Text = "No " + AppResources.FavoriteMoviesPivotTitle + " available";
                                                               tblkMoviesHistory.Visibility = Visibility.Visible;
                                                           }
                                                       }
                                                     );
                    bwHelper.RunBackgroundWorkers();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in UserControl_Loaded Method In ShowHistory file.", ex);
            }
        }
    }
}
